#include "OptionsMenu.h"
#include "../Engine.h"

OptionsMenu::OptionsMenu()
{
    screenName = "OptionsMenu";

    int verticalSpace = 50;

    // GUI Tests

    // Checkbox
    checkbox = new GUICheckbox();
    addChild(checkbox);

    checkbox->x = 50;
    checkbox->y = 50;
    checkbox->setChecked(true);

    form.add(checkbox);

    // Text Button
    button = new GUITextButton();
    addChild(button);

    button->x = 50;
    button->y = checkbox->y + verticalSpace;
    button->width = 200;
    button->height = 25;
    button->setText("Hello world");

    form.add(button);

    // Checkbox
    checkbox2 = new GUICheckbox();
    addChild(checkbox2);

    checkbox2->x = 50;
    checkbox2->y = button->y + verticalSpace;
    checkbox2->setChecked(true);

    form.add(checkbox2);

    // Modify current clear color
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}

void OptionsMenu::draw()
{

}

void OptionsMenu::update(Uint32* deltaTime)
{

}

void OptionsMenu::handleEvents(SDL_Event* event)
{
    if (event->type == SDL_KEYDOWN)
    {
        // Tab between gui elements in the form
        if (event->key.keysym.sym == SDLK_TAB)
        {
            // Note: We can't use key.keysym.mod == SDLK_LSHIFT unless the SDL unicode option is enabled.
            if (SDL_GetModState() & KMOD_SHIFT)
            {
                form.previous();
            }
            else
            {
                form.next();
            }
        }
        else if (event->key.keysym.sym == SDLK_UP || event->key.keysym.sym == SDLK_LEFT)
        {
            form.previous();
        }
        else if (event->key.keysym.sym == SDLK_DOWN || event->key.keysym.sym == SDLK_RIGHT)
        {
            form.next();
        }
        else if (event->key.keysym.sym == SDLK_RETURN)
        {
            form.onPressed();
        }
        // Move back to main screen
        else if (event->key.keysym.sym == SDLK_ESCAPE)
        {
            // Create and add new screen
            MainMenu* menu = new MainMenu();
            Engine::Instance().addScreen2D(menu);
            // Turn this screen off
            visible = updatable = eventable = false;
        }
    }
    else if (event->type == SDL_KEYUP)
    {
        if (event->key.keysym.sym == SDLK_RETURN)
        {
            form.onReleased();
        }
    }
}
